package com.dataseek.iot.product.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.product.entity.PdtCategory;
import com.dataseek.iot.product.service.CtgService;
import com.dataseek.iot.product.vo.CtgVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/category")
public class CtgController {

    @Autowired
    private CtgService ctgService;


    /**
     * 查询类别列表
     *
     * @param ctgName 类别名称
     * @return
     */

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult queryCtg(@RequestParam(value="ctgName", required=false) String ctgName ) throws IOException {
        List<CtgVo> ctgVo = ctgService.queryCtg(ctgName);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),ctgVo);
    }

    /**
     * 修改类别列表
     *
     * @param pdtCategory 类别实体类
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult updateCtg(@RequestBody PdtCategory pdtCategory) throws IOException {
        Integer code  = ctgService.updateCtg(pdtCategory);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

    /**
     * 新建类别
     *
     * @param pdtCategory 类别实体类
     * @return
     */

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insertCtg(@RequestBody PdtCategory pdtCategory) throws IOException, SQLException {
        Integer code  = ctgService.insertCtg(pdtCategory);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"类别编码字段重复，请重新输入!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        return responseResult;
    }


    /**
     * 删除类别
     *
     * @param ids 类别ID,满足批量删除
     * @return
     */

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult deleteCtg(@RequestBody JSONObject ids)  throws IOException {
        JSONArray jsonArray = ids.getJSONArray("ids");
        List<Long> idList = JSONObject.parseArray(jsonArray.toJSONString(), Long.class);
        Integer code  = ctgService.deleteCtg(idList);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult = new ResponseResult(RspStatusEnum.FAIL.getCode(),"该类别在使用中，无法删除!",code.toString());
        } else {
            responseResult = new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
        }
        return responseResult;
    }



}
